#ifndef PACKAGE_H
#define PACKAGE_H

#include "types.h"
#include "stm32f10x.h"

// USART connected to Bluetooth
#define USART				USART1
#define USART_BAUDRATE		921600
#define USART_RX_BUFSIZE	64

// Sensor Package struct
typedef struct {
	char  		sync0;
	char  		sync1;
	char  		pkgLengthInWORD;
	uint16_t  	command;
	uint16_t 	*buffer;
	uint16_t 	checkSum;
} SensorXPackage_t;

typedef enum {
	//*********************************************
	//*				INPUT PACKAGES
	//*********************************************
	
	//* DataLength = 1
	//* Data[0] = Channel index
	PKG_ENABLE_TEMP 			= (uint16_t)0x0031,
	
	//* DataLength = 1;
	//* Data[0] = Channel index
	PKG_ENABLE_TIME				= (uint16_t)0x0032,
	
	//* DataLength = 0;
	PKG_ENABLE_GRAVITY			= (uint16_t)0x0033,
	
	//* DataLength = 1;
	//* Data[1] = Gravity range
	PKG_SELECT_GRAVITY_RANGE	= (uint16_t)0x0034,
	
	//* DataLength = 2;
	//* Data[0] = Address
	//* Data[1] = Value
	PKG_WRITE_GRAVITY_REG		= (uint16_t)0x0035,
	
	//* DataLength = 1;
	//* Data[0] = Address
	//* Value in RESULT package
	PKG_READ_GRAVITY_REG		= (uint16_t)0x0036,
	
	//* DataLength = 2;
	//* Data[0] = Channel
	//* Data[1] = Sample period in Millisecond
	PKG_SET_SAMPLE_PERIOD		= (uint16_t)0x0037,
	
	//* DataLength = 1;
	//* Data[0] = FREQ = (MAX)0 ~ 7(MIN) 
	PKG_SET_GRAVITY_FREQ		= (uint16_t)0x0038,
	
	//* DataLength = 1;
	//* Data[0] = Channel
	PKG_ENABLE_FREQ				= (uint16_t)0x0039,
	
	//* Disable Command Mask
	PKG_DISABLE_MASK			= (uint16_t)0x8000, 
	
	//*********************************************
	//*				OUTPUT PACKAGES
	//*********************************************
	
	//* DataLength = 4
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = Command Value
	//* Data[3] = Command result: Passed<0>, Failed<1>
	//* Data[n] = Extra Datas * n
	PKG_COMMAND_RESULT			= (uint16_t)0x0800,
	
	//* DataLength = 5
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = <MSB8bits> Sensor Type | <LSB8bits> ChannelIndex
	//*		  	  Digital<0> Analog<1>
	//* Data[3] = Temperature Value 1<Digital: Temp16, Analog: Volt1 
	//* Data[4] = Temperature Value 2<Digital: Dummy,  Analog: Volt2
	PKG_TEMP_DATA				= (uint16_t)0x0041,
	
	//* DataLength = 3 
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = <MSB8bits> IO value | <LSB8bits> Channel index	
	PKG_TIME_DATA				= (uint16_t)0x0042,
	
	//* DataLength = 5 
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = X-Axis value
	//* Data[3] = Y-Axis value
	//* Data[4] = Z-Axis value
	PKG_GRAVITY_DATA			= (uint16_t)0x0043,
	
	//* DataLength = 3 
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = Tick Count Value
	PKG_HEART_DATA				= (uint16_t)0x0044,
	
	//* DataLength = 3 
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = Battery ADC Value
	PKG_BATTERY_DATA			= (uint16_t)0x0045,
	
	//* DataLength = 4 
	//* Data[0] = Time Mark LSB in MS
	//* Data[1] = Time Mark MSB in MS
	//* Data[2] = Channel
	//* Data[3] = Frequency LSB
	//* Data[4] = Frequency MSB
	PKG_FREQ_DATA				= (uint16_t)0x0046,
} PKG_CLASS;

extern void WritePackage(PKG_CLASS command, uint32_t time, uint16 *buffer, uint8_t bufferLen);
extern void WriteCmdResponse(PKG_CLASS cmd_value, int result, uint16_t *buffer, uint8_t bufferLength);
extern int PackageListener(void);
extern void RegisterPackageHandler(void (*fn)(SensorXPackage_t *));
extern void UnRegisterPackageHandler(void);


#endif // ifndef PACKAGE_H
